package cool.taomu.software.fig.sshutils

import com.jcraft.jsch.ChannelExec
import com.jcraft.jsch.ChannelSftp
import com.jcraft.jsch.JSch
import com.jcraft.jsch.Session
import com.jcraft.jsch.SftpProgressMonitor
import java.io.ByteArrayOutputStream
import java.util.Properties
import org.eclipse.xtend.lib.annotations.Accessors
import org.eclipse.xtend.lib.annotations.ToString
import org.slf4j.LoggerFactory

class SshUtils implements AutoCloseable {
    val LOG = LoggerFactory.getLogger(SshUtils);
    @Accessors
    @ToString
    static class ConfigEntity{
       String username; 
       String password;
       String host;
       Integer port;
       new(String username,String password,String host,Integer port){
          this.username = username; 
          this.password = password;
          this.host = host;
          this.port = port;
       }
    }

    Session session;

    new(ConfigEntity config) {
        var jsch = new JSch();
        session = jsch.getSession(config.username, config.host, config.port);
        session.password = config.password;
        var _config = new Properties();
        _config.put("StrictHostKeyChecking", "no");
        session.setConfig(_config)
        session.timeout = 60000000;
        session.connect();
        if (session.connected) {
            LOG.info("ssh session connect success")
        } else {
            LOG.info("ssh session connect fail")
        }
    }

    def check(String dir, String cmd) {
        try {
            var exec = session.openChannel("exec") as ChannelExec;
            var cmd1 = String.format("cd %s;%s", dir, cmd);
            exec.setCommand(cmd1);
            exec.inputStream = null;
            exec.setErrStream(System.err);
            exec.connect();
            var input = exec.inputStream;
            var bos = new ByteArrayOutputStream();
            // TODO StreamUtils.copy(input, bos);
            var String content = new String(bos.toByteArray);
            input.close;
            exec.disconnect;
            return content;
        } catch (Exception ex) {
            ex.printStackTrace;
        }
    }

    def sftp_get(String dir, String filename) {
        var sftp = session.openChannel("sftp") as ChannelSftp;
        sftp.connect;
        sftp.cd(dir)
        var bos = new ByteArrayOutputStream();
        sftp.get(filename, bos, new SftpProgressMonitor() {
            long transfered;
            long max = 0;

            override count(long count) {
                transfered = transfered + count;
                var d = transfered / (max as double) * 100;
                println(String.format("%s %d %d %.2f", filename, this.max, transfered, d))
                return true;
            }

            override end() {
                println("done.");
            }

            override init(int op, String src, String dest, long max) {
                this.max = max;
            }

        });
        var String content = new String(bos.toByteArray);
        sftp.disconnect;
        return content;
    }

    override close() throws Exception {
        session.disconnect;
    }

}
